package com.angel.provider.health.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.angel.core.config.PDFValueConfig;
import com.angel.core.util.JsonUtils;
import com.angel.dao.health.BuzSurveyMapper;
import com.angel.health.BuzSurvey;
import com.angel.health.ln.ChartMap;
import com.angel.provider.health.BuzSurveyProvider;

import net.sf.json.JSONArray;

@Service
public class BuzSurveyProviderImpl implements BuzSurveyProvider {

	/*@Autowired
	private MongoTemplate template;*/
	@Autowired
	private BuzSurveyMapper mapper;

	public int save(BuzSurvey survey) {
		try {
			Date d = new Date();  
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	        String date = sdf.format(d);  
			survey.setSurveyTime(new Date());
			survey.setDateTime(date);
			mapper.insert(survey);
			//template.save(survey);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

		public static void main(String[] args) {
			
		}




	@Override
	public BuzSurvey findByCustomerId(String id) {
		// TODO Auto-generated method stub
		return mapper.findByCustomerId(id);
	}
	
	public void updateSurvey(BuzSurvey survey){
		mapper.updateByPrimaryKeySelective(survey);
	}

	@Override
	public Map<String,String>  PDFByCustomerId(String id) {
		Map<String,String> showMap  = new HashMap<String,String>();
		BuzSurvey b = mapper.findByCustomerId(id);
		Map<String,String> map = new HashMap<String,String>();
		
		String basis = b.getContentBasis();
		if(basis!=null){
			JSONArray jsonAr = JSONArray.fromObject(basis);
			Map<String,String> ct = null;
			for(int i=0;i<jsonAr.size();i++){
				ct = JsonUtils.toMap(jsonAr.get(i).toString());
				map.put((ct.get("key").split("_"))[2], ct.get("value"));
			}
		}
		
		String tional = b.getContentTional();
		if(tional!=null){
			JSONArray jsonAr = JSONArray.fromObject(tional);
			Map<String,String> ct = null;
			for(int i=0;i<jsonAr.size();i++){
				ct = JsonUtils.toMap(jsonAr.get(i).toString());
				map.put((ct.get("key").split("_"))[2], ct.get("value"));
			}
		}
		String chemical = b.getContentChemical();
		if(chemical!=null){
			JSONArray jsonAr = JSONArray.fromObject(chemical);
			Map<String,String> ct = null;
			for(int i=0;i<jsonAr.size();i++){
				ct = JsonUtils.toMap(jsonAr.get(i).toString());
				map.put((ct.get("key").split("_"))[2], ct.get("value"));
			}
		}
		String psy = b.getContentPsy();
		if(psy!=null){
			JSONArray jsonAr = JSONArray.fromObject(psy);
			Map<String,String> ct = null;
			for(int i=0;i<jsonAr.size();i++){
				ct = JsonUtils.toMap(jsonAr.get(i).toString());
				map.put((ct.get("key").split("_"))[2], ct.get("value"));
			}
		}
		String women = b.getContentWomen();
		if(women!=null){
			JSONArray jsonAr = JSONArray.fromObject(women);
			Map<String,String> ct = null;
			for(int i=0;i<jsonAr.size();i++){
				ct = JsonUtils.toMap(jsonAr.get(i).toString());
				map.put((ct.get("key").split("_"))[2], ct.get("value"));
			}
		}
		String doctor = b.getContentDoctor();
		if(doctor!=null){
			JSONArray jsonAr = JSONArray.fromObject(doctor);
			Map<String,String> ct = null;
			for(int i=0;i<jsonAr.size();i++){
				ct = JsonUtils.toMap(jsonAr.get(i).toString());
				map.put((ct.get("key").split("_"))[2], ct.get("value"));
			}
		}
		
		Map<String,String> family = new HashMap<String,String>();
		String[][] fm = PDFValueConfig.family;
		String familyStr = "";
		for(int i=0;i<fm.length;i++){
			if(map.get(fm[i][0])!=null){
				family.put(fm[i][0], fm[i][1]+":"+map.get(fm[i][0]));
				familyStr +=fm[i][1]+",";
			}
		}
		Map<String,String> history = new HashMap<String,String>();
		String[][] hs = PDFValueConfig.history;
		String historyStr = "";
		for(int i=0;i<hs.length;i++){
			if(map.get(hs[i][0])!=null){
				history.put(hs[i][0], hs[i][1]+":"+map.get(hs[i][0]));
				historyStr +=hs[i][1] +",";
			}
		}
		Map<String,String> normal = new HashMap<String,String>();
		String[][] nr = PDFValueConfig.nor;
		for(int i=0;i<nr.length;i++){
			if(map.get(nr[i][0])!=null){
				if("six".equals(nr[i][0])){
					if("1".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "男");
					}else if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "女");
					}else{
						normal.put(nr[i][0], "其他");
					}
				}else if("age".equals(nr[i][0])){
					normal.put(nr[i][0], map.get(nr[i][0]));
				}else if("smork".equals(nr[i][0])){
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "以前吸，但现在已经戒烟");
					}else if("3".equals(map.get(nr[i][0]))){
						if("1".equals(map.get("smorkhz"))){
							normal.put(nr[i][0], "每天吸");
						}
						else if("2".equals(map.get("smorkhz"))){
							normal.put(nr[i][0], "经常吸");
						}else{
							normal.put(nr[i][0], "偶尔吸");
						}
					}else if("1".equals(map.get(nr[i][0]))){
						if("1".equals(map.get("drink"))){
							normal.put(nr[i][0], "被动吸烟");
						}
					}
				}else if("beforeDrink".equals(nr[i][0])){
					if("1".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "喝过酒");
						if("3".equals(map.get("drinkMonthMorning"))){
							normal.put(nr[i][0], "经常喝");
						}else if("4".equals(map.get("drinkMonthMorning"))){
							normal.put(nr[i][0], "天天喝");
						}else{
							normal.put(nr[i][0], "偶尔喝");
						}
					}else{
						normal.put(nr[i][0], "从不喝酒");
					}
				}else if("height".equals(nr[i][0])){
					 normal.put(nr[i][0], map.get(nr[i][0]));
				}else if("culture".equals(nr[i][0])){
					if("1".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "小学");
					}
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "初中");
					}
					if("3".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "中专");
					}
					if("4".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "高中");
					}
					if("5".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "大专");
					}
					if("6".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "本科");
					}
					if("7".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "研究生");
					}
					if("8".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "博士");
					}
					if("9".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "其他");
					}
				}else if("taste".equals(nr[i][0])){
					if("1".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "偏咸");
					}
				}else if("exercisingWay".equals(nr[i][0])){
					if("1".equals(map.get("keepPhysical"))){
						normal.put(nr[i][0], "良好");
					}else if("2".equals(map.get("keepPhysical"))){
						normal.put(nr[i][0], "一般");
					}else{
						normal.put(nr[i][0], "低");
					}
				}else if("dh".equals(nr[i][0])){
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "膳食结构合理");
					}else{
						normal.put(nr[i][0], "膳食结构不合理");
					}
				}else if("mastopathyHistory".equals(nr[i][0])){
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "否");
					}
					if("3".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "是");
					}
				}else if("occasionallyOil".equals(nr[i][0])){
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "否");
					}
					if("3".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "是");
					}
				}else if("oftenOil".equals(nr[i][0])){
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "否");
					}
					if("3".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "是");
					}
				}else if("estrogen".equals(nr[i][0])){
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "否");
					}
					if("1".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "是");
					}
				}else if("firstMenstrualAge".equals(nr[i][0])){
					if("1".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "12");
					}
					if("2".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "13");
					}
					if("3".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "14");
					}
					if("4".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "15");
					}
					if("5".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "16");
					}
					if("6".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "17");
					}
					if("7".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "18");
					}
					if("8".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "19");
					}
					if("9".equals(map.get(nr[i][0]))){
						normal.put(nr[i][0], "20");
					}
				}else if("firstMarriage".equals(nr[i][0])){
					normal.put(nr[i][0], map.get(nr[i][0]));
				}
				else{
					normal.put(nr[i][0], nr[i][1]+":"+map.get(nr[i][0]));
				}
			}
		}
		if(nr!=null)
		showMap.put("normal", JsonUtils.toJson(normal));
		if(historyStr.length()>0)
		showMap.put("history", historyStr.substring(0,historyStr.length()-1));
		if(familyStr.length()>0)
		showMap.put("family", familyStr.substring(0,familyStr.length()-1));
		return showMap;
	}






	@Override
	public List<ChartMap> loadChart() {
		// TODO Auto-generated method stub
		return mapper.loadChart();
	}

}
